home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / alpha / strchr.c < prev    next >
C/C++ Source or Header  |  1993-12-22  |  3KB  |  81 lines

  1. /* Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7.  
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. Library General Public License for more details.
  12.  
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB.  If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA.  */
  17.  
  18. #include <string.h>
  19.  
  20. /* Return the length of the null-terminated string STR.  Scan for
  21.    the null terminator quickly by testing eight bytes at a time.  */
  22.  
  23. char *
  24. strchr (const char *str, int c)
  25. {
  26.   const char *char_ptr;
  27.   const unsigned long int *longword_ptr;
  28.   unsigned long int charmask;
  29.  
  30.   c = (unsigned char) c;
  31.  
  32.   /* Handle the first few characters by reading one character at a time.
  33.      Do this until STR is aligned on a 8-byte border.  */
  34.   for (char_ptr = str; ((unsigned long int) char_ptr & 7) != 0; ++char_ptr)
  35.     if (*char_ptr == c)
  36.       return (char *) char_ptr;
  37.     else if (*char_ptr == '\0')
  38.       return NULL;
  39.  
  40.   longword_ptr = (unsigned long int *) char_ptr;
  41.  
  42.   /* Set up a longword, each of whose bytes is C.  */
  43.   charmask = c | (c << 8);
  44.   charmask |= charmask << 16;
  45.   charmask |= charmask << 32;
  46.   charmask |= charmask << 64;
  47.  
  48.   for (;;)
  49.     {
  50.       const unsigned long int longword = *longword_ptr++;
  51.       int ge, le, zero;
  52.  
  53.       /* Set bits in ZERO if bytes in LONGWORD are zero.  */
  54.       asm ("cmpbge $31, %1, %0" : "=r" (zero) : "r" (longword));
  55.  
  56.       /* Set bits in GE if bytes in CHARMASK are >= bytes in LONGWORD.  */
  57.       asm ("cmpbge %1, %2, %0" : "=r" (ge) : "r" (charmask), "r" (longword));
  58.  
  59.       /* Set bits in LE if bytes in CHARMASK are <= bytes in LONGWORD.  */
  60.       asm ("cmpbge %2, %1, %0" : "=r" (le) : "r" (charmask), "r" (longword));
  61.  
  62.       /* Bytes that are both <= and >= are == to C.  */
  63.       if (zero || (ge & le))
  64.     {
  65.       /* Which of the bytes was the C?  */
  66.  
  67.       char *cp = (char *) (longword_ptr - 1);
  68.       int i;
  69.  
  70.       for (i = 0; i < 8; i++)
  71.         {
  72.           if (cp[i] == c)
  73.         return &cp[i];
  74.           if (cp[i] == 0)
  75.         return NULL;
  76.         }
  77.       return NULL;
  78.     }
  79.     }
  80. }
  81.